// All onload javascript and jquery will goes here
function calcParallax(tileheight, speedratio, scrollposition) {
	return (tileheight - (Math.floor(scrollposition / speedratio) % (tileheight + 1)));
}
function launch() {
	$('#login-signup').lightbox_me({
		centered: true, 
		onLoad: function() { 
			$('#login-signup').find('input:first').focus()
		}
	});
}
function exit() {
	$('#login-signup').trigger('close');
}

function passwordStrength(password)
{				
	var score = 0;
	//if password has not entered no warning
	if (password.length == 0) { score = "";}
	//if password bigger than 6 give 1 point
	if (password.length > 6) score++;
	//if password has both lower and uppercase characters give 1 point      
	if (( password.match(/[a-z]/)) && ( password.match(/[A-Z]/))) score++;
	//if password has at least one number give 1 point
	if (password.match(/\d+/)) score++;
	//if password has at least one special caracther give 1 point
	if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) score++;
	//if password bigger than 12 give another 1 point
	if (password.length > 12) score++;
	document.getElementById("passStrength").className = "strength" + score;
}

$(function() {
	// This is for the parellax rolling of the background
	window.onscroll = function() {
		var posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop
				: window.pageYOffset;

		var plane = document.getElementById('back-layer');
		var planeparallax = calcParallax(3000, 8, posY);
		plane.style.backgroundPosition = "0 " + planeparallax
				+ "px";

		var midplane = document.getElementById('fore-layer');
		var midplaneparallax = calcParallax(2800, 3, posY);
		midplane.style.backgroundPosition = "0 "
				+ midplaneparallax + "px";
	};
	
	// Navigation
	var $el, leftPos, newWidth, $mainNav = $("#nav_group");

	$mainNav.append("<li id='nav_line_bottom'></li>");
	var $nav_line_bottom = $("#nav_line_bottom");

	$nav_line_bottom.data("origLeft", $nav_line_bottom.position().left).data("origWidth", $nav_line_bottom.width());
	
	$("#nav_group li a").hover(function() {
		$el = $(this);
		leftPos = $el.position().left;
		newWidth = $el.parent().width();
		$nav_line_bottom.stop().animate({
			left : leftPos,
			width : newWidth
		});
	}, function() {
		$nav_line_bottom.stop().animate({
			left : $nav_line_bottom.data("origLeft"),
			width : $nav_line_bottom.data("origWidth")
		});
	});
	
	// Form input
	$("input").focus(function() {
		$(this).parent().addClass("curr")
	});
	$("input").blur(function() {
		$(this).parent().removeClass("curr")
	});
	
	// Tabs
	$(".tab").click(function() {
		var X=$(this).attr('id');
		
		if(X=='signuptab') {
			$("#logintab").removeClass('active');
			$("#signuptab").addClass('active');
			$("#loginbox").slideUp();
			$("#signupbox").slideDown();
		} else {
			$("#signuptab").removeClass('active');
			$("#logintab").addClass('active');
			$("#signupbox").slideUp();
			$("#loginbox").slideDown();
		}
	});
	
	// Masked input
	$(".phone").mask("(999) 999-9999? x9999", {placeholder:"_"});
	
	//Validations
	var form = $("#form");
	var name = $("#user");
	var nameInfo = $("#userInfo");
	var username = $("#username");
	var usernameInfo = $("#usernameInfo");
	var pass1 = $("#pass");
	var pass1Info = $("#passInfo");
	var pass2 = $("#conf_pass");
	var pass2Info = $("#conf_passInfo");
	var password = $("#password");
	var passwordInfo = $("#passwordInfo");
	var message = $("#message");
	var email1 = $("#email");
	var email1Info = $("#emailInfo");
	var email2 = $("#conf_email");
	var email2Info = $("#conf_emailInfo");
	var phoneNumber = $("#phoneNumber");
	var phoneNumberInfo = $("#phoneNumberInfo");
	
	//On blur
	name.blur(validateName);
	email1.blur(validateEmail1);
	email2.blur(validateEmail2);
	pass1.blur(validatePass1);
	pass2.blur(validatePass2);
	username.blur(validateUsername);
	password.blur(validatePassword);
	//On key press
	name.keyup(validateName);
	pass1.keyup(validatePass1);
	pass2.keyup(validatePass2);
	email1.keyup(validateEmail1);
	email2.keyup(validateEmail2);
	username.keyup(validateUsername);
	password.keyup(validatePassword);
	
	//On Submitting
	function checkRegister()
	{
		
	}
	function checkLogin()
	{
		if(validateUsername() && validatePassword())
			return true;
		else
			return false;
	}
	form.submit(function(){
		if((validateName() & validateEmail1() & validatePass1() & validatePass2() & validateEmail1() & validateEmail2()) | (validateUsername() & validatePassword()))
			return true;
		else
			return false;
	});
	
	//validation functions
	function validateEmail1(){
		//testing regular expression
		var a = $("#email").val();
		var b = $("#email1");
		var c = $("#email2");
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email1.removeClass("error");
			email1.addClass("valid");
			email1Info.text("Email Is Valid!");
			email1Info.removeClass("error");
			email1Info.addClass("valid");
			validateEmail2();
			return true;
		}
		//if it's NOT valid
		else{
			email1.removeClass("valid");
			email1.addClass("error");
			email1Info.text("Invalid Email!");
			email1Info.removeClass("valid");
			email1Info.addClass("error");
			return false;
		}
	}
	function validateEmail2(){
		//testing regular expression
		var a = $("#conf_email").val();
		var b = $("#email1");
		var c = $("#email2");
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		
		//if it's valid email
		if(filter.test(a)){
			email2.removeClass("error");
			email2.addClass("valid");
			email2Info.text("Email Matched!");
			email2Info.removeClass("error");
			email2Info.addClass("valid");
			return true;
		}
		else{
			if(	email1.val() != email2.val()) {
				email2.removeClass("valid");
				email2.addClass("error");
				email2Info.text("Emails Mismatched!");
				email2Info.removeClass("valid");
				email2Info.addClass("error");
				return false;
			}
			else if (email2.val().length == 0) {
				email2.removeClass("valid");
				email2.addClass("error");
				email2Info.text("Cannot Be Empty!");
				email2Info.removeClass("valid");
				email2Info.addClass("error");
				return false;
			}
			else {
				email2.removeClass("valid");
				email2.addClass("error");
				email2Info.text("Invalid Email!");
				email2Info.removeClass("valid");
				email2Info.addClass("error");
				return false;
			}
		}
	}
	function validateName(){
		//if it's NOT valid
		if(name.val().length < 4){
			name.removeClass("valid");
			name.addClass("error");
			nameInfo.text("Must Be At Least 4 Characters");
			nameInfo.removeClass("valid");
			nameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			name.removeClass("error");
			name.addClass("valid");
			nameInfo.text("Username Is Valid");
			nameInfo.removeClass("error");
			nameInfo.addClass("valid");
			return true;
		}
	}
	function validateUsername(){
		//if it's NOT valid
		if(username.val().length == 0){
			username.removeClass("valid");
			username.addClass("error");
			usernameInfo.text("Please Enter A Username");
			usernameInfo.removeClass("valid");
			usernameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			username.removeClass("error");
			username.addClass("valid");
			usernameInfo.text("Username Entered");
			usernameInfo.removeClass("error");
			usernameInfo.addClass("valid");
			return true;
		}
	}
	function validatePassword(){
		//if it's NOT valid
		if(password.val().length == 0){
			password.removeClass("valid");
			password.addClass("error");
			passwordInfo.text("Please Enter A Password");
			passwordInfo.removeClass("valid");
			passwordInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			password.removeClass("error");
			password.addClass("valid");
			passwordInfo.text("Password Entered");
			passwordInfo.removeClass("error");
			passwordInfo.addClass("valid");
			return true;
		}
	}
	function validatePass1(){
		var a = $("#password1");
		var b = $("#password2");

		//it's NOT valid
		if(pass1.val().length < 8){
			pass1.removeClass("valid");
			pass1.addClass("error");
			pass1Info.text("Must Be At Least 8 Characters!");
			pass1Info.removeClass("valid");
			pass1Info.addClass("error");
			return false;
		}
		//it's valid
		else{			
			pass1.removeClass("error");
			pass1.addClass("valid");
			pass1Info.text("123 + !@# + Abc = Strong");
			pass1Info.removeClass("error");
			pass1Info.addClass("valid");
			validatePass2();
			return true;
		}
	}
	function validatePass2(){
		var a = $("#password1");
		var b = $("#password2");
		//are NOT valid
		if( pass1.val() != pass2.val() ) {
			pass2.removeClass("valid");
			pass2.addClass("error");
			pass2Info.text("Passwords Mismatched!");
			pass2Info.removeClass("valid");
			pass2Info.addClass("error");
			return false;
		}
		else if (pass2.val().length == 0) {
			pass2.removeClass("valid");
			pass2.addClass("error");
			pass2Info.text("Empty Field!");
			pass2Info.removeClass("valid");
			pass2Info.addClass("error");
		}
		//are valid
		else{
			pass2.removeClass("error");
			pass2.addClass("valid");
			pass2Info.text("Password Matched!");
			pass2Info.removeClass("error");
			pass2Info.addClass("valid");
			return true;
		}
	}
});
